package cn.com.xy.sms.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.entity.MatchCacheManager;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.title.SmsTitleManager;
import cn.com.xy.sms.sdk.util.PopupUtil;
import cn.com.xy.sms.sdk.util.SceneconfigDownloaderQueue;

public class ParseCardWatchManager {

    public static JSONObject parseMsgForCardWatch(Context ctx,
            String phoneNumber, String smsCenterNum, String smsContent,
            Map<String, String> extend) throws Exception {
        if (ctx == null) {
            throw new Exception(" Context is null.");
        }

        if (phoneNumber == null) {
            throw new Exception(" phoneNumber is null.");
        }
        if (smsContent == null) {
            throw new Exception(" smsContent is null.");
        }
        JSONObject jsonObject = null;
        try {
            Map<String, Object> res = ParseManager.parseMsg(ctx, phoneNumber,
                    smsCenterNum, smsContent, 0l, extend);

            if (res != null && ParseBubbleManager.getParseStatu(res) != -1) {

                Map<String, Object> handresult = DexUtil
                        .handerBubbleValueMap(res);
                if (handresult == null || handresult.isEmpty())
                    return null;

                JSONObject watchRes = DexUtil.handerWatchValueMap(handresult);
                if (watchRes != null && watchRes.length() > 0) {
                    handresult.put("watchJsObj", watchRes);
                }

                jsonObject = parseMsgToBubbleCardResult(handresult, extend);

                // if(isNeedRes(extend)){
                // }
                /*
                 * else { return changeMapToJson(handresult); }
                 */

            }
        } catch (Throwable e) {
            // TODO: handle exception
        }

        return jsonObject;
    }

    private static boolean isNeedRes(Map<String, String> extend) {
        if (extend == null)
            return true;
        if (!extend.containsKey("isNeedRes"))
            return true;
        String isNeedRes = extend.get("isNeedRes");
        return "true".equalsIgnoreCase(isNeedRes);
    }

    private static JSONObject changeMapToJson(Map<String, Object> handresult) {
        JSONObject jsobj = new JSONObject();
        Set<Entry<String, Object>> set = handresult.entrySet();
        for (Entry<String, Object> entry : set) {
            try {
                jsobj.put(entry.getKey(), entry.getValue());
            } catch (JSONException e) {
//                e.printStackTrace();
                LogManager.e(Constant.TAG, "changeMapToJson: ", e);

                return null;
            }
        }
        MatchCacheManager.removeUselessKey(jsobj);
        return jsobj;
    }

    private static JSONObject parseMsgToBubbleCardResult(
            Map<String, Object> res, Map<String, String> extend)
            throws Exception {
        try {

            if (res != null) {
                String titleNo = (String) res.get("title_num");
                HashMap<String, String> bubleSmsTitle = SmsTitleManager
                        .getBubleSmsTitle(titleNo);

                boolean bubleSmsTitleIsNotNull = bubleSmsTitle != null
                        && !bubleSmsTitle.isEmpty();

                if (!isNeedRes(extend)
                        || (bubleSmsTitleIsNotNull && PopupUtil.isPopupAble(
                                res, titleNo))) {
                    if (bubleSmsTitle != null) {
                        res.putAll(bubleSmsTitle);
                    }
                    return changeMapToJson(res);
                }
            }

        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "parseMsgToBubbleCardResult: ", e);

        } finally {
            // 触发情景check队列，情景down队列，资源down队列
            SceneconfigDownloaderQueue.runSceneconfigDownloadQueue();
        }
        return null;
    }

}
